package com.lc.app.androidfirst.userdefine.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.app.androidfirst.R;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SDExplorerActivity extends AppCompatActivity {

    ListView listView;
    TextView textView;
    File currentParent;
    File[] currentFiles;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdexplorer);

        listView = (ListView) findViewById(R.id.listView);
        textView = (TextView) findViewById(R.id.textViewShowPath);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = (int) (dm.widthPixels*0.8);

        File root = new File("/");
        if(root.exists()){
            currentParent = root;
            currentFiles = root.listFiles();
            inflateListView(currentFiles);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(currentFiles[position].isFile()){
                    return ;
                }
                File[] tmp = currentFiles[position].listFiles();
                if(tmp == null || tmp.length == 0){
                    Toast.makeText(getApplicationContext(),"当前路路径不可访问或者该路径下没有文件。",Toast.LENGTH_SHORT).show();
                }else{
                    currentParent = currentFiles[position];
                    currentFiles = tmp;
                    inflateListView(currentFiles);
                }
            }
        });

        Button btnReturnUp = (Button) findViewById(R.id.btnReturnUp);
        btnReturnUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File tmp = currentParent.getParentFile();
                if(tmp == null){
                    Toast.makeText(getApplicationContext(),"已经是根目录。",Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    currentFiles = tmp.listFiles();
                    currentParent = tmp;
                    inflateListView(currentFiles);
                }
            }
        });

    }




    private void inflateListView(File[] files){
        List<Map<String,Object>> listItems = new ArrayList<>();
        for (int i=0;i<files.length;i++){
            Map<String,Object> itemMap = new HashMap<>();
            if(files[i].isDirectory()){
                if(files[i].listFiles() != null){
                    itemMap.put("icon",R.drawable.icon_folder);
                }else{
                    itemMap.put("icon",R.drawable.icon_empty_folder);
                }

            }else{
                itemMap.put("icon", R.drawable.icon_file);
            }
            itemMap.put("fileName",files[i].getName());
            listItems.add(itemMap);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.content_line,new String[]{"icon","fileName"},new int[]{R.id.listImage,R.id.listFileName});
        listView.setAdapter(simpleAdapter);
        try {
            textView.setText("当前路径为："+currentParent.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
